package com.jlc.book.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jlc.book.shop.delegate.UserDelegate;
import com.jlc.book.shop.to.UserTO;
import com.jlc.book.shop.validator.JLCDataValidator;

public class ChangePasswordAction {
	public String changePassword(HttpServletRequest req,HttpServletResponse res){
		String page="changePasswordDef.jsp";
		String newpass=req.getParameter("newpass");
		String currpass=req.getParameter("currpass");
		String confpass=req.getParameter("confpass");
		
		boolean compareEquals=true;
		boolean intractWithDB=true;
		
		/*VALIDATING NEW PASSWORD*/
		if(!JLCDataValidator.validateRequired(newpass)){
			req.setAttribute("newpass","Newpass is required");
			intractWithDB=false;
			compareEquals=false;
		}else if(!JLCDataValidator.minLength(newpass, 6)){
			req.setAttribute("newpass","Newpass must be minimum 6 charater long");
			intractWithDB=false;
			compareEquals=false;
		}else if(!JLCDataValidator.maxLength(newpass, 15)){
			req.setAttribute("newpass","Newpass must be maximum 15 charater long");
			intractWithDB=false;
			compareEquals=false;
		}
		/*VALIDATING CURRENT PASSWORD*/
		if(!JLCDataValidator.validateRequired(newpass)){
			req.setAttribute("currpass","Currentpass is required");
			intractWithDB=false;
			compareEquals=false;
		}
		/*VALIDATING CONFIRM PASSWORD*/
		if(!JLCDataValidator.validateRequired(confpass)){
			req.setAttribute("confpass","Confpass is required");
			intractWithDB=false;
			compareEquals=false;
		}else if(!JLCDataValidator.minLength(confpass, 6)){
			req.setAttribute("newpass","Confpass must be minimum 6 charater long");
			intractWithDB=false;
			compareEquals=false;
		}else if(!JLCDataValidator.maxLength(confpass, 15)){
			req.setAttribute("newpass","Confpass must be maximum 15 charater long");
			intractWithDB=false;
			compareEquals=false;
		}
		if(compareEquals){
			if(!newpass.equals(confpass)){
				intractWithDB=false;
				req.setAttribute("changePasswordError","New & Confirm Password doesnot match");
			}
		}
		if(intractWithDB){
			Object obj=req.getSession().getAttribute("USER_TO");
			UserTO usto=(UserTO)obj;
			if(currpass.equals(usto.getPassword())){
				UserTO uto=UserDelegate.changePassword(usto,newpass);
				if(uto!=null){
					req.getSession().setAttribute("USER_TO",uto);
					req.setAttribute("changePasswordError","Password Updated Successfully");
					req.setAttribute("CHANGE_PASSWORD","OK");
				}else{
					req.setAttribute("changePasswordError","Error in Changing Password");
				}
			}else{
				req.setAttribute("changePasswordError","Current Password is invalid");
			}
		}
		return page;
	}
}
